
import {ref,toRefs} from 'vue'
import { getDicts } from '@/api/dict'
import store from '@/store'
// console.log(store);
export function useDict (...args) {
  const res = ref({});
  return (() => {
    args.forEach(async (dictType, index) => {
      res.value[dictType] = [];
      const dicts = await store.dispatch("getDict", dictType);
      if (dicts) {
        res.value[dictType] = dicts;
      } else {
        getDicts(dictType).then(resp => {
          res.value[dictType] = resp.data.map(d => ({
            label: d.dictLabel,
            value: d.dictValue
          }));
          store.dispatch("setDict", {
            key: dictType,
            value: res.value[dictType]
          });
        });
      }
    });
    return toRefs(res.value);
  })();
};
